﻿Public Class clsDistanceCalc
    Dim PassedPositions As Integer = 0
    Dim OldLat As Double = -1
    Dim OldLon As Double = -1

    Private _Distance As Double = 0
    Public Property Distance() As Double
        Get
            Return _Distance
        End Get
        Private Set(ByVal value As Double)
            _Distance = value
        End Set
    End Property

    ''' <summary>
    ''' A simple algorith to calculate the distance between 2 GPS points
    ''' </summary>
    ''' <param name="GPSPosition">GpsPosition for the new location</param>
    ''' <remarks>The function will automatically save the old point, you need at least 2 points to get the distance between.
    ''' and we get the distance every 200 points to minimize the errors and noise IF the Speed is less than 3 knot.
    ''' </remarks>
    <CLSCompliant(False)> _
    Public Sub Push(ByVal GPSPosition As Microsoft.WindowsMobile.Samples.Location.GpsPosition)
        Try
            PassedPositions += 1

            Dim PosUtil As New clsPosition(GPSPosition)
            Dim Speed50 As Double = modUtil.Map(PosUtil.Speed, 0, 100, 0, 50)
            Dim Accuracy35 As Double = modUtil.Map(PosUtil.PositionAccuracy, 0, 100, 0, 35)
            Dim SatCount15 As Double = modUtil.Map(PosUtil.SatCount, 0, 9, 0, 15)

            Dim TotalWeight As Double = Speed50 + Accuracy35 + SatCount15

            Dim PointCount As Integer = CInt(modUtil.Map(TotalWeight, 0, 100, 25, 1))

            If PosUtil.Speed <= 3 Then
                PointCount = 200 'to avoid counting for low speed
            End If

            If PassedPositions >= PointCount Then
                PassedPositions = 0
                'Calc Distance
                If OldLat = -1 Then
                    OldLat = GPSPosition.Latitude
                    OldLon = GPSPosition.Longitude
                Else
                    Me.Distance += GetDist(OldLat, OldLon, GPSPosition.Latitude, GPSPosition.Longitude)
                    OldLat = GPSPosition.Latitude
                    OldLon = GPSPosition.Longitude
                    'MsgBox("Calc")
                End If
            End If
        Catch ex As Exception
            PushLog("Error in clsDistanceCalc.Push: " & ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Private function to get the distance between 2 Lat and Lon
    ''' </summary>
    ''' <param name="Lat1">Lat for point 1</param>
    ''' <param name="Lon1">Lon for point 1</param>
    ''' <param name="Lat2">Lat for point 2</param>
    ''' <param name="Lon2">Lon for point 2</param>
    ''' <returns>Return the distance in knot</returns>
    ''' <remarks></remarks>
    Private Function GetDist(ByVal Lat1 As Double, ByVal Lon1 As Double, ByVal Lat2 As Double, ByVal Lon2 As Double) As Double
        Try
            Lat1 = Math.PI * Lat1 / 180
            Lon1 = Math.PI * Lon1 / 180
            Lat2 = Math.PI * Lat2 / 180
            Lon2 = Math.PI * Lon2 / 180
            Dim V As Double
            V = 6371000 * Math.Acos(Math.Cos(Lon1 - Lon2) * Math.Cos(Lat1) * Math.Cos(Lat2) + Math.Sin(Lat1) * Math.Sin(Lat2))

            If V.ToString.ToLower = "nan" Then
                Return 0
            Else
                Return V
            End If
        Catch ex As Exception
            PushLog("Error in clsDistanceCalc.GetDist: " & ex.Message)
        End Try
    End Function

    Public Sub ResetDistance()
        Me.Distance = 0
    End Sub
End Class
